package com.baidu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baidu.constant.MessageConstant;
import com.baidu.entity.PageResult;
import com.baidu.entity.QueryPageBean;
import com.baidu.entity.Result;
import com.baidu.pojo.Permission;
import com.baidu.service.PermissionService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/authority")
public class PermissionController {
	@Reference
	private PermissionService permissionService;

	@RequestMapping("/pageQuery") //我们最终返回是一个pageResult对象，该对象有总记录，当前页的结果。
	public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
		//System.out.println(queryPageBean.getCurrentPage()+"//"+queryPageBean.getPageSize()+"//"+queryPageBean.getQueryString());
		PageResult pageResult = permissionService.pageQuery(queryPageBean);
		//System.out.println(pageResult.getRows()+"//"+pageResult.getTotal());
		return pageResult;

	}



	@RequestMapping("/add")
	public Result add(@RequestBody Permission permission){


		try {
			Result result = permissionService.add(permission);

			return result;
		} catch (Exception e) {
			return new Result(false,MessageConstant.ADD_PERMISSION_FAIL);
		}


	}


	@DeleteMapping("/delete/{id}")
	public Result delete( @PathVariable Integer id){


		try {
			Result result = permissionService.delete(id);

			return  result;
		} catch (Exception e) {
			return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
		}

	}

	@RequestMapping("/findById")
	public Result findById(Integer id){

		try {
			Permission permission= permissionService.findById(id);
		return 	 new Result(true,MessageConstant.GET_PERMISSION_SUCCESS,permission);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 	 new Result(false,MessageConstant.GET_PERMISSION_FAIL);

	}

	@RequestMapping("/edit")
		public Result edit(@RequestBody Permission permission){


		try {
			Result result = permissionService.edit(permission);
			return result;
		} catch (Exception e) {
		return 	new Result(false,MessageConstant.EDIT_PERMISSION_FAIL);


		}

	}



}
